﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar;

using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Arithmetic;

namespace PlanningGIS.CoordCovert.form
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmProGroCoordConvert : Office2007Form
    {
       /// <summary>
       /// 
       /// </summary>
        public FrmProGroCoordConvert()
        {
            InitializeComponent();
            SetInputState(true);
            txtGS2JWX.Tag = false;
            txtGS2JWY.Tag = false;
            txtJW2GSLat.Tag = false;
            txtJW2GSLong.Tag = false;
            txtDegreeLat.Tag = false;
            txtDegreeLong.Tag = false;
            txtMinLat.Tag = false;
            txtMinLong.Tag = false;
            txtSecLat.Tag = false;
            txtSecLong.Tag = false;
            cboGS2JW.SelectedIndex = 0;
            cboJW2GS.SelectedIndex = 0;
            ValidateCalculateBtn();
        }

        //经纬度转高斯界面下设置输入格式文本的状态
        private void SetInputState(bool blEnable)
        {
            txtJW2GSLat.Visible = blEnable;
            txtJW2GSLong.Visible = blEnable;


            txtDegreeLat.Visible = !blEnable;
            lblDegreeLat.Visible = !blEnable;

            txtDegreeLong.Visible = !blEnable;
            lblDegreeLong.Visible = !blEnable;

            txtMinLat.Visible = !blEnable;
            lblMinLat.Visible = !blEnable;

            txtMinLong.Visible = !blEnable;
            lblMinLong.Visible = !blEnable;

            txtSecLat.Visible = !blEnable;
            lblSecLat.Visible = !blEnable;

            txtSecLong.Visible = !blEnable;
            lblSecLong.Visible = !blEnable;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            double L, B, X, Y, L0, S;
            int D, M;
            L = 0;
            B = 0;
            X = 0;
            Y = 0;
            L0 = 0;
            D = 0;
            M = 0;
            S = 0;
            try
            {
                switch (tabConver.SelectedTabIndex)
                {
                    case 0:
                        X = Convert.ToDouble((string) txtGS2JWX.Text);
                        Y = Convert.ToDouble((string) txtGS2JWY.Text);
                        L0 = Convert.ToDouble((string) cboGS2JW.Text);

                        if (true == rdoGS2JW54.Checked)
                            CoordSystemHelper.XY2BL54(X, Y, ref B, ref L, L0);
                        else if (rdoGS2JW80.Checked == true)
                            CoordSystemHelper.XY2BL80(X, Y, ref B, ref L, L0);

                        if (rdoGS2JWdeg.Checked == true)
                        {
                            txtGS2JWLong.Text = Convert.ToString(L);
                            txtGS2JWLat.Text = Convert.ToString(B);
                        }
                        else if (rdoGS2JWdms.Checked == true)
                        {
                            txtGS2JWLong.Text = CoordSystemHelper.DEG2DMS(L);
                            txtGS2JWLat.Text = CoordSystemHelper.DEG2DMS(B);
                        }
                        break;

                    case 1:
                        if (rdoJW2GSdeg.Checked == true)
                        {
                            L = Convert.ToDouble((string) txtJW2GSLong.Text);
                            B = Convert.ToDouble((string) txtJW2GSLat.Text);
                        }
                        else if (rdoJW2GSdms.Checked == true)
                        {
                            D = Convert.ToInt32((string) txtDegreeLong.Text);
                            M = Convert.ToInt32((string) txtMinLong.Text);
                            S = Convert.ToDouble((string) txtSecLong.Text);
                            L = CoordSystemHelper.DMS2DEG(D, M, S);

                            D = Convert.ToInt32((string) txtDegreeLat.Text);
                            M = Convert.ToInt32((string) txtMinLat.Text);
                            S = Convert.ToDouble((string) txtSecLat.Text);
                            B = CoordSystemHelper.DMS2DEG(D, M, S);
                        }
                        L0 = Convert.ToDouble((string) cboJW2GS.Text);
                        if (rdoJW2GS54.Checked == true)
                            CoordSystemHelper.BL2XY54(B, L, ref X, ref Y, L0);
                        else if (rdoJW2GS80.Checked == true)
                            CoordSystemHelper.BL2XY80(B, L, ref X, ref Y, L0);

                        txtJW2GSX.Text = Convert.ToString(X);
                        txtJW2GSY.Text = Convert.ToString(Y);

                        break;
                    default:
                        break;
                }
            }
            catch (Exception excep)
            {
                MsgHelper.ShowWarningMsgbox(excep.Message);
            }
        }

        //文本由度分秒转换到度
        private void TXTdms2deg(TextBox txtd, TextBox txtm, TextBox txts, TextBox txtdeg)
        {
            int d = 0, m = 0;
            double deg = 0, s = 0;
            if (txtd.Text != "" && txtm.Text != "" && txts.Text != "")
            {
                d = Convert.ToInt32(txtd.Text);
                m = Convert.ToInt32(txtm.Text);
                s = Convert.ToDouble(txts.Text);

                deg = CoordSystemHelper.DMS2DEG(d, m, s);
                txtdeg.Text = Convert.ToString(deg);
            }
        }

        //文本由度分秒转换到度
        private void TXTdms2deg(TextBox txtDDD)
        {
            int d = 0, m = 0;
            double s = 0;
            string strTxt = txtDDD.Text;

            int indexd = 0, indexm = 0, indexs = 0;
            indexd = strTxt.IndexOf('°');
            indexm = strTxt.IndexOf('′');
            indexs = strTxt.IndexOf('″');

            d = Convert.ToInt32(strTxt.Substring(0, indexd));
            m = Convert.ToInt32(strTxt.Substring(indexd + 1, indexm - indexd - 1));
            s = Convert.ToDouble(strTxt.Substring(indexm + 1, indexs - indexm - 1));

            txtDDD.Text = Convert.ToString(CoordSystemHelper.DMS2DEG(d, m, s));
        }

        //文本由度转换到度分秒
        private void TXTdeg2dms(TextBox txtdeg, TextBox txtd, TextBox txtm, TextBox txts)
        {
            int d = 0, m = 0;
            double deg = 0, s = 0;

            if (txtdeg.Text != "")
            {
                deg = Convert.ToDouble(txtdeg.Text);
                CoordSystemHelper.DEG2DMS(deg, ref d, ref m, ref s);
                txtd.Text = Convert.ToString(d);
                txtm.Text = Convert.ToString(m);
                txts.Text = Convert.ToString(s);
            }
        }

        private void rdoJW2GSdeg_Click(object sender, EventArgs e)
        {
            SetInputState(true);
            TXTdms2deg(txtDegreeLat, txtMinLat, txtSecLat, txtJW2GSLat);
            TXTdms2deg(txtDegreeLong, txtMinLong, txtSecLong, txtJW2GSLong);
        }

        private void rdoJW2GSdms_Click(object sender, EventArgs e)
        {
            SetInputState(false);
            TXTdeg2dms(txtJW2GSLat, txtDegreeLat, txtMinLat, txtSecLat);
            TXTdeg2dms(txtJW2GSLong, txtDegreeLong, txtMinLong, txtSecLong);
        }


        private void rdoGS2JWdeg_CheckedChanged(object sender, EventArgs e)
        {
            if (txtGS2JWLat.Text != "" && txtGS2JWLong.Text != "")
            {
                double Deg;
                if (rdoGS2JWdeg.Checked == true)
                {
                    TXTdms2deg(txtGS2JWLat);
                    TXTdms2deg(txtGS2JWLong);
                }
                else if (rdoGS2JWdms.Checked == true)
                {
                    Deg = Convert.ToDouble((string) txtGS2JWLat.Text);
                    txtGS2JWLat.Text = CoordSystemHelper.DEG2DMS(Deg);


                    Deg = Convert.ToDouble((string) txtGS2JWLong.Text);
                    txtGS2JWLong.Text = CoordSystemHelper.DEG2DMS(Deg);
                }
            }
        }

        private void txtBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox) sender;
            if (tb.Text.Length == 0)
            {
                tb.Tag = false;
                //tb.BackColor = Color.Red;
            }
            else
            {
                tb.Tag = true;
                tb.BackColor = SystemColors.Window;
            }

            ValidateCalculateBtn();
        }

        //输入字符只能是数字或退格
        private void txtIntBox_KeyPress(object sender,
                                        KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
            if ((ModifierKeys & Keys.Control) == Keys.Control)
                e.Handled = false;
        }

        //输入字符只能是数字、小数点或退格
        private void txtDoubleBox_KeyPress(object sender,
                                           KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
                e.Handled = true;
            if ((ModifierKeys & Keys.Control) == Keys.Control)
                e.Handled = false;
        }


        private void ValidateCalculateBtn()
        {
            btnCalculate.Enabled = (tabConver.SelectedTabIndex == 0)
                                       ? ((bool) txtGS2JWX.Tag && (bool) txtGS2JWY.Tag)
                                       : ((rdoJW2GSdeg.Checked == true)
                                              ? ((bool) txtJW2GSLat.Tag && (bool) txtJW2GSLong.Tag)
                                              :
                                                  ((bool) txtDegreeLat.Tag && (bool) txtDegreeLong.Tag &&
                                                   (bool) txtMinLat.Tag &&
                                                   (bool) txtMinLong.Tag && (bool) txtSecLat.Tag &&
                                                   (bool) txtSecLong.Tag));
        }

        private void tabConver_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateCalculateBtn();
        }

        private void txtGS2JWLong_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtGS2JWLat_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}